//
//  StringHelper.m
//  testWebView
//
//  Created by ty804 on 04/05/2019.
//  Copyright © 2019 ty804. All rights reserved.
//

#import "StringHelper.h"
#import <objc/runtime.h>
#import <objc/message.h>


@interface StringHelper ()
@property (nonatomic,strong)NSArray *arr_class_type;

@end

@implementation StringHelper

static StringHelper * _instance = nil;
+(StringHelper *) sharedManage;{
    @synchronized(self){
        if (_instance == nil) {
            _instance = [[self alloc] init];
        }
    }
    return  _instance;
}

-(void)traverse_souceCode_souceCode:(NSString *)code back:(void (^_Nonnull)(NSArray<SouceModel*> *model))classModel;{
    NSString *strCode=code;
    NSArray *arrCode=[strCode componentsSeparatedByString:@"\n"];
    NSMutableArray *arrModel=[NSMutableArray array];
   __block NSUInteger index=0;
    __block NSUInteger i=0;
    NSMutableArray *arrPragma=[[NSMutableArray alloc]init];
    
    [arrCode enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj localizedStandardContainsString:@"#pragma mark"]) {
            [arrPragma addObject:obj];
            if (i==0) {
                SouceModel *model=[[SouceModel alloc]init];
                NSArray *arr=[NSArray array];
                arr= [arrCode subarrayWithRange:NSMakeRange(index, idx-index)];
                model.arrSouce=arr;
                model.className=@"";
                model.classType=@"";
                [arrModel addObject:model];
                index=idx;
                
            }else{
                NSString *obj_pragma=arrPragma[i-1];
                SouceModel *model=[[SouceModel alloc]init];
                NSArray *arr=[NSArray array];
                arr= [arrCode subarrayWithRange:NSMakeRange(index, idx-index)];
                model.arrSouce=arr;
                NSArray *arrClass=[obj_pragma componentsSeparatedByString:@"*"];
                model.className=arrClass[1];
                model.classType=arrClass.lastObject;
                [arrModel addObject:model];
                index=idx;
            }
            
            i++;
        }
        if (idx==arrCode.count-1) {
            NSString *obj_pragma=arrPragma[i-1];
            SouceModel *model=[[SouceModel alloc]init];
            NSArray *arr=[NSArray array];
            arr= [arrCode subarrayWithRange:NSMakeRange(index, idx-index)];
            model.arrSouce=arr;
            NSArray *arrClass=[obj_pragma componentsSeparatedByString:@"*"];
            model.className=arrClass[1];
            model.classType=arrClass.lastObject;
            [arrModel addObject:model];
        }
    }];
    
    if (arrModel.count>0) {
        classModel(arrModel);
    }
}
//新增类别，转换为SouceModel
-(void)addClassModel:(NSInteger)index totleModel:(NSArray *)arrModel back:(void (^_Nonnull)(NSArray<SouceModel*> *model,NSArray<ClassModel*> *classmodel))classModel{
    NSMutableArray *arrClassModel=[NSMutableArray arrayWithCapacity:index];
    NSMutableArray *arrSouceModel=[NSMutableArray arrayWithCapacity:index];
    for (int i=0; i<index; i++) {
        ClassModel *model=[[ClassModel alloc]init];
        [model setDefautInfo];
        [arrClassModel addObject:model];
        SouceModel *model1=[[SouceModel alloc]init];
        model1.arrSouce=[model.classSouce componentsSeparatedByString:@"\n"];
        model1.className=model.className;
        model1.classType=model.classType;
        [arrSouceModel addObject:model1];
    }
    NSMutableArray *arrT=[NSMutableArray arrayWithArray:arrModel];
    NSRange range = NSMakeRange(4, arrSouceModel.count);
    NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
    [arrT insertObjects:arrSouceModel atIndexes:set];
    classModel(arrT,arrClassModel);
}
//添加随机属性和调用
-(void)addPrameterWithNum:(NSInteger)index ratio:(NSInteger)ratio model:(SouceModel *)model back:(void (^_Nonnull)(SouceModel *model))classModel;{
    NSNumber *num=[NSNumber numberWithInteger:arc4random_uniform(index)+1];
    model.index=num.intValue;
    model.ratio=ratio;
    [model insertStatementCode];
    [model insertFunctionCode];
    classModel(model);
}

-(void)addClassPrameter:(NSArray<SouceModel *> *)arrSouceModel classModel:(NSArray<ClassModel *> *)classModel back:(void (^_Nonnull)(NSArray<SouceModel *> *model))arrClassModel;{
    int index=0;
    if (classModel.count>0) {
        ClassModel*model=classModel.lastObject;
        for (SouceModel *souceModel in arrSouceModel) {
            if ([souceModel.className isEqualToString:model.className]) {
                NSNumber *num=[NSNumber numberWithUnsignedInteger:[arrSouceModel indexOfObject:souceModel]];
                index=num.intValue;
            }
        }
    }
    NSMutableArray *arrInput=[[NSMutableArray alloc]init];
    for (int i=0; i<arrSouceModel.count; i++) {
        if (i>index) {
            SouceModel *souceModel=arrSouceModel[i];
            if (![souceModel.classType isEqualToString:@"NSObject"]) {
                [arrInput addObject:souceModel];
            }
        }
        
    }
    
    
    
    float num_1=[[NSNumber numberWithInteger:classModel.count] floatValue]/[[NSNumber numberWithInteger:arrInput.count] floatValue];
    int a=[NSNumber numberWithFloat:num_1].intValue;
    NSArray *arr=[self splitArray:classModel withSize:a];
    [arrInput enumerateObjectsUsingBlock:^(SouceModel*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    
        if (idx==arrInput.count-1) {
            NSRange range = NSMakeRange(idx, arr.count-idx);
            NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
            NSArray *arr1=[arr objectsAtIndexes:set];
            NSMutableArray *arr3=[[NSMutableArray alloc]init];
            for (NSArray *arr2 in arr1) {
                [arr3 addObjectsFromArray:arr2];
            }
            [obj insertClassCode:arr3];
            
        }else{
            [obj insertClassCode:arr[idx]];
        }
       
//        [arr_num1 enumerateObjectsUsingBlock:^(ClassModel * _Nonnull obj_model, NSUInteger idx_num, BOOL * _Nonnull stop) {
//
//            if (num_1>=1) {
//
//                if (index_num==arrInput.count-1) {
//
//                  [arr_num2 addObjectsFromArray:arr_num1];
//
//                }else{
//                    while (idx_num<=num_1-1) {
//                        [arr_num2 addObject:obj_model];
//                        idx_num++;
//                        *stop =YES;
//                        [arr_num1 removeObject:obj_model];
//                        *stop =NO;
//                    }
//                }
//
//            }else{
//
//                [arr_num2 addObjectsFromArray:classModel];
//            }
//            index_num++;
//        }];
        
        
        
//        NSInteger count=arc4random() % [classModel count];
//
//        if (count==0) {
//            count++;
//        }
//    //随机从 class数组取出 个数，不能重复取一个，所以要去重
//       NSMutableSet *randomSet = [[NSMutableSet alloc] init];
//        while ([randomSet count] < count) {
//            int r = arc4random() % count;
//            [randomSet addObject:[classModel objectAtIndex:r]];
//        }
//        NSArray *randomArray = [randomSet allObjects];
        
        
        
        
    }];
    arrClassModel(arrSouceModel);
}
-(NSArray *)splitArray:(NSArray *)array withSize:(NSInteger)subSize{
    unsigned long count =array.count % subSize ==0 ? (array.count /subSize) :(array.count / subSize+1);
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    for (int i=0; i<count; i++) {
        int index=i * subSize;
        NSMutableArray *arr1=[[NSMutableArray alloc]init];
        [arr1 removeAllObjects];
        int j=index;
        while (j<subSize*(i+1) &&j<array.count) {
            [arr1 addObject:[array objectAtIndex:j]];
            j+=1;
        }
        [arr addObject:[arr1 copy]];
    }
    return  arr.copy;
}
//合并代码
-(void)restructuringCode:(NSArray<SouceModel*> *)arrModel back:(void (^_Nonnull)(NSString *code))backCode;{
    
    __block NSMutableArray *arr_last=[[NSMutableArray alloc]init];
    [arrModel enumerateObjectsUsingBlock:^(SouceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [arr_last addObjectsFromArray:obj.arrSouce];
    }];
    backCode([arr_last componentsJoinedByString:@"\n"]);
}


-(void)traverse_souceCode_classModel:(NSString *)code back:(void (^_Nonnull)(NSString *className,NSArray *arrFunctionCode))classModel;{
    
    NSString *strCode=code;
    NSArray *arrCode=[strCode componentsSeparatedByString:@"\n"];
    NSMutableArray *arrClass=[NSMutableArray array];
    
    [arrCode enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj localizedStandardContainsString:@"@interface"]) {
            NSArray *arr=[obj componentsSeparatedByString:@":"];
            if (arr.count==2) {
                NSString *name=[[arr firstObject] stringByReplacingOccurrencesOfString:@"@interface" withString:@""];
                NSString *className=[name stringByReplacingOccurrencesOfString:@" " withString:@""];
                [arrClass addObject:className];
            }else{
                NSArray *arr=[obj componentsSeparatedByString:@"("];
                if (arr.count==2) {
                    NSString *name=[[arr firstObject] stringByReplacingOccurrencesOfString:@"@interface" withString:@""];
                    NSString *className=[name stringByReplacingOccurrencesOfString:@" " withString:@""];
                    [arrClass addObject:className];
                }
            }
        }
    }];
    

}
+ (void)runTests:(NSString *)class
{
    unsigned int methCount = 0;
    Method *meths = class_copyMethodList([NSClassFromString(class) class], &methCount);
    
    for(int i = 0; i < methCount; i++) {
        
        Method meth = meths[i];
        
        SEL sel = method_getName(meth);
        
        const char *name = sel_getName(sel);
        
        NSLog(@"%s", name);
    }
    
    free(meths);
}
+(NSString *)subString:(NSString *)souceString From:(NSString *)startString to:(NSString *)endString{
    
    NSRange startRange = [souceString rangeOfString:startString];
    NSRange endRange = [souceString rangeOfString:endString];
    NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
    return [souceString substringWithRange:range];
}
@end
